package com.yuke.cloud.service.wmc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.annotation.LogAnnotation;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.core.utils.RequestUtil;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.pmc.dto.*;
import com.yuke.cloud.service.api.pmc.service.PmcFeignApi;
import com.yuke.cloud.service.wmc.dto.*;
import com.yuke.cloud.service.wmc.entity.WmsStorage;
import com.yuke.cloud.service.wmc.entity.WmsWarehouse;
import com.yuke.cloud.service.wmc.service.*;
import com.yuke.cloud.service.wmc.util.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * Created by zys on 2018/11/26.
 */
@Api(value = "WEB - WmsStorageController", tags = "处理WmsStorage相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/wms/storage")
@Slf4j
public class WmsStorageController extends BaseController {
    @Resource
    private WmsStorageService wmsStorageService;
    @Resource
    private WmsStorageRecordService wmsStorageRecordService;
    @Resource
    private PmcFeignApi pmcFeignApi;
    @Resource
    private WmsStockInService wmsStockInService;
    @Resource
    private WmsStockOutService wmsStockOutService;
    @Resource
    private WmsWarehouseService wmsWarehouseService;


    @PostMapping("/getSpecBySpecName")
    public Wrapper<PageInfo<WmsStorage>> getSpecBySpecName(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                           @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                           @RequestBody ProdSpecInfoDto prodSpecInfoDto) {
        Example example = new Example(WmsStorage.class);
        Example.Criteria criteria = example.createCriteria();

        if (prodSpecInfoDto.getIsEnable() != null ){
            criteria.andEqualTo("isEnable",prodSpecInfoDto.getIsEnable());
        }
        if (prodSpecInfoDto.getCategoryId() != null && prodSpecInfoDto.getCategoryId() > 0){
            criteria.andEqualTo("categoryId",prodSpecInfoDto.getCategoryId());
        }
        if (prodSpecInfoDto.getWarehouseId() != null && prodSpecInfoDto.getWarehouseId() > 0){
            criteria.andEqualTo("warehouseId",prodSpecInfoDto.getWarehouseId());
        }
        if (prodSpecInfoDto.getSpecName() != null && prodSpecInfoDto.getSpecName() != ""){
            criteria.andLike("specName","%"+prodSpecInfoDto.getSpecName()+"%");
        }
        PageHelper.startPage(page, size);
        List<WmsStorage>  wmsStorageList = wmsStorageService.selectByExample(example);
        PageInfo pageInfo = new PageInfo(wmsStorageList);
        return WrapMapper.ok(pageInfo);
    }


    @PostMapping("/save")
    @LogAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "wmsStorage", value = "该数据记录的POJO") @RequestBody WmsStorage wmsStorage) {
        System.out.println("========"+wmsStorage);
        Long userId = RequestUtil.getLoginUser().getUserId();
        wmsStorage.setUpdateId(userId);
        wmsStorageService.save(wmsStorage);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @LogAnnotation
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        wmsStorageService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @LogAnnotation
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "wmsStorage", value = "需要修改的内容") @RequestBody WmsStorage wmsStorage) {
        Long userId = getLoginAuthDto().getUserId();
        wmsStorage.setUpdateId(userId);
        wmsStorageService.update(wmsStorage);
        return WrapMapper.ok();
    }

    @PostMapping("/updateEntity")
    @LogAnnotation
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper updateEntity(@ApiParam(name = "wmsStorage", value = "需要修改的内容") @RequestBody WmsStorage wmsStorage) {
        if (wmsStorage!=null){
            Example example = new Example(WmsStorage.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("specId",wmsStorage.getSpecId());
            criteria.andEqualTo("warehouseId",wmsStorage.getWarehouseId());
            System.out.println(wmsStorage);
            Long userId = getLoginAuthDto().getUserId();
            wmsStorage.setUpdateId(userId);
            wmsStorage.setUpdateTime(new Date());
            wmsStorageService.updateByExample(wmsStorage,example);
        }
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<WmsStorage> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        WmsStorage wmsStorage = wmsStorageService.findById(id);
        return WrapMapper.ok(wmsStorage);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<WmsStorage>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                              @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<WmsStorage> list = wmsStorageService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }


    //入库记录查看
    @GetMapping("/searchStockIn")
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<WmsStockInDto>> search1(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                 @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                 @ApiParam(name = "warehouseId", value = "仓库id", defaultValue = "0") @RequestParam(required = false) Long warehouseId,
                                                 @ApiParam(name = "specId", value = "产品id", defaultValue = "0") @RequestParam(required = false) Long specId,
                                                 @ApiParam(name = "inType", value = "入库类型", defaultValue = "0") @RequestParam(required = false) Byte inType,
                                                 @ApiParam(name = "beginTime", value = "开始时间", defaultValue = "0") @RequestParam(required = false) Long beginTime,
                                                 @ApiParam(name = "endTime", value = "结束时间", defaultValue = "0") @RequestParam(required = false) Long endTime,
                                                 @ApiParam(name = "minPrice", value = "低价", defaultValue = "0") @RequestParam(required = false) Integer minPrice,
                                                 @ApiParam(name = "maxPrice", value = "高价", defaultValue = "0") @RequestParam(required = false) Integer maxPrice,
                                                 @ApiParam(name = "maxPrice", value = "高价", defaultValue = "0") @RequestParam(required = false) Boolean isDelete,
                                                 @ApiParam(name = "purchaseId", value = "单号", defaultValue = "0") @RequestParam(required = false) Long purchaseId



    ) throws ParseException {

        WmsStockInDto wmsStockInDto = new WmsStockInDto();
        if (warehouseId!=null){
            wmsStockInDto.setWarehouseId(warehouseId);
        }
        Byte wareCategory=0;

        List<Long> warehouseIds = getWarehouseIdList(wareCategory);
        if (warehouseIds != null){
            wmsStockInDto.setWarehouseIdList(warehouseIds);
        }

        if (purchaseId!=null && purchaseId!=0){
            wmsStockInDto.setPurchaseId(purchaseId);
        }

        if (isDelete!=null){
            wmsStockInDto.setIsDelete(isDelete);
        }

        //大于等于
        if (beginTime != null && !beginTime.equals("")) {
            wmsStockInDto.setBeginTime(new Date(beginTime));
        }
        //小于等于
        if (endTime != null && !endTime.equals("")) {
            wmsStockInDto.setEndTime(new Date(endTime));
        }
        if (specId != null && !specId.equals("")) {
            wmsStockInDto.setSpecId(specId);
        }
        if (inType != null && !inType.equals("")) {
            wmsStockInDto.setInType(inType);
        }
        if (specId != null && !specId.equals("")) {
            wmsStockInDto.setSpecId(specId);
        }
        if (minPrice!=null && !minPrice.equals(0)){
            wmsStockInDto.setMinPrice(minPrice);
        }
        if (maxPrice!=null && !maxPrice.equals(0)){
            wmsStockInDto.setMaxPrice(maxPrice);
        }
        PageHelper.startPage(page, size);
        List<WmsStockInDto> list = wmsStockInService.getWmsStockInByCondition(wmsStockInDto);
        if (list.size() == 0) {
            List list1 = new ArrayList();
            PageInfo pageInfo = new PageInfo(list1);
            return WrapMapper.ok(pageInfo);
        }

        List<Long> specList = new ArrayList<>();
        for (WmsStockInDto stockInDto : list) {
            specList.add(stockInDto.getSpecId());
        }


        List<Long> productIds = list.stream().map(WmsStockInDto::getProductId).collect(Collectors.toList());

        List<ProdSpecInfoDto> prodNameByProductId = pmcFeignApi.getProdNameByProductId(productIds);
        if (prodNameByProductId == null||prodNameByProductId.size()==0) {
            return WrapMapper.error("服务异常");
        }

        List<Long> categoryList = prodNameByProductId.stream().map(ProdSpecInfoDto::getCategoryId).collect(Collectors.toList());

        List<ProdSpecApiDto> parent = pmcFeignApi.findParent(categoryList);

        for (WmsStockInDto item : list) {
            for (ProdSpecInfoDto prodSpecInfoDto : prodNameByProductId) {
                if (item.getProductId().toString().equals(prodSpecInfoDto.getProductId().toString()) ) {
                    item.setProductName(prodSpecInfoDto.getProductName());
                    item.setCategoryName(prodSpecInfoDto.getCategoryName());
                    item.setParentId(prodSpecInfoDto.getParentId());
                    item.setCategoryId(prodSpecInfoDto.getCategoryId());
                }
            }
            for (ProdSpecApiDto prodSpecApiDto : parent) {
                if (prodSpecApiDto.getCategoryId().equals(item.getCategoryId())) {
                    item.setParentName(prodSpecApiDto.getParentName());
                    item.setParentId(prodSpecApiDto.getParentId());
                }
            }

        }
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }



    //出库记录查看
    @GetMapping("/searchStockOut")
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<WmsStockOutDto>> search(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                    @ApiParam(name = "warehouseId", value = "仓库id", defaultValue = "0") @RequestParam(required = false) Long warehouseId,
                                                    @ApiParam(name = "specId", value = "产品规格id", defaultValue = "0") @RequestParam(required = false) Long specId,
                                                    @ApiParam(name = "outType", value = "出库类型", defaultValue = "0") @RequestParam(required = false) Byte outType,
                                                    @ApiParam(name = "beginTime", value = "开始时间", defaultValue = "0") @RequestParam(required = false)  Long beginTime,
                                                    @ApiParam(name = "endTime", value = "结束时间", defaultValue = "0") @RequestParam(required = false)  Long endTime,
                                                    @ApiParam(name = "endTime", value = "结束时间", defaultValue = "0") @RequestParam(required = false)  Long purchaseReturnId,
                                                    @ApiParam(name = "orderId", value = "orderId", defaultValue = "0") @RequestParam(required = false)  Long orderId


    ) throws ParseException {

        WmsStockOutDto wmsStockOutDto = new WmsStockOutDto();
        if (warehouseId!=null){
            wmsStockOutDto.setWarehouseId(warehouseId);
        }

        if(getGroupInfoDto().getGroupId()!=null && getGroupInfoDto().getGroupId()>1){
            WmsWarehouse wmsWarehouse = new  WmsWarehouse();
            // 登录人为3级部门
            if (getGroupInfoDto().getLevel().toString().equals("3")){
                wmsWarehouse.setGroupId(getGroupInfoDto().getParentId());
            }else{
                wmsWarehouse.setGroupId(getGroupInfoDto().getGroupId());
            }

            List<WmsWarehouseDto> wmsWarehouseList = wmsWarehouseService.getWarehouseInfoList(wmsWarehouse);


            if (wmsWarehouseList != null && wmsWarehouseList.size()>0){
                List<Long> warehouseIdList = wmsWarehouseList.stream().map(WmsWarehouseDto::getWarehouseId).collect(Collectors.toList());
                wmsStockOutDto.setWarehouseIdList(warehouseIdList);
            }
        }

        //大于等于
        if (outType != null && !outType.equals("")) {
            wmsStockOutDto.setOutType(outType);
        }
        //小于等于
        if (specId != null && !specId.equals("")) {
            wmsStockOutDto.setSpecId(specId);
        }
        if (beginTime != null && !beginTime.equals("") && endTime != null && !endTime.equals("")) {
            wmsStockOutDto.setBeginTime(new Date(beginTime));
            wmsStockOutDto.setEndTime(new Date(endTime));
        }

        if(purchaseReturnId !=null &&purchaseReturnId!=0){
            wmsStockOutDto.setPurchaseReturnId(purchaseReturnId);
        }

        if (orderId!=null && orderId !=0){
            wmsStockOutDto.setOrderId(orderId);
        }
        System.out.println("参数stockOut:"+wmsStockOutDto);
        PageHelper.startPage(page, size);
        List<WmsStockOutDto> list = wmsStockOutService.getWmsStockOutByCondition(wmsStockOutDto);
        if (list.size() == 0) {
            List list1 = new ArrayList();
            PageInfo pageInfo = new PageInfo(list1);
            return WrapMapper.ok(pageInfo);
        }
        List<Long> productIds = list.stream().map(WmsStockOutDto::getProductId).collect(Collectors.toList());

        List<ProdSpecInfoDto> prodNameByProductId = pmcFeignApi.getProdNameByProductId(productIds);
        if (prodNameByProductId == null||prodNameByProductId.size()==0) {
            return WrapMapper.error("服务异常");
        }

        for (WmsStockOutDto item : list) {
            for (ProdSpecInfoDto prodSpecInfoDto : prodNameByProductId) {
                if (item.getProductId().toString().equals(prodSpecInfoDto.getProductId().toString())){
                    item.setProductName(prodSpecInfoDto.getProductName());
                    item.setCategoryName(prodSpecInfoDto.getCategoryName());
                }
            }
        }
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    //添加商品入库
    /*
    只传入warehouseId 和 productId和specId
     */
    @PostMapping("/addGoods")
    @LogAnnotation
    @SneakyThrows
    @ApiOperation(httpMethod = "POST", value = "根据productId入库")
    public Wrapper addGoodsStorage(@RequestBody ProdStorageDto prodStorageDto){

        if (prodStorageDto == null){
            return WrapMapper.error("参数为空！");
        }
        if (prodStorageDto.getProductId() == null ){
              return WrapMapper.wrap(Wrapper.ERROR_CODE,"传入参数错误，productId 为 null");
        }
        if (prodStorageDto.getWarehouseId() == null ){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"传入参数错误，warehouseId 为 null");
        }
        Long userId = getLoginAuthDto().getUserId();
        prodStorageDto.setUpdateId(userId);

        //获得当前产品的所有规格spec的信息
        List<ProdSpecInfoExDto> prodSpecInfoExDtoList = pmcFeignApi.getSpecIds(prodStorageDto);
        //添加到库存记录表
        CompletableFuture<Integer> recorder = wmsStorageRecordService.addStorageRecorder(prodStorageDto,prodSpecInfoExDtoList);
        //添加到库存商品表
        CompletableFuture<Integer> goodsStorage =wmsStorageService.addGoodsStorage(prodStorageDto,prodSpecInfoExDtoList);

        // 等待所有任务都执行完
        CompletableFuture.allOf(recorder, goodsStorage).join();
        // 获取每个任务的返回结果
        Integer result = recorder.get() + goodsStorage.get();
        return WrapMapper.ok(result);
    }

    @PutMapping("/update")
    @LogAnnotation
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper updateStorage(@ApiParam(name = "wmsStorage", value = "需要修改的内容") @RequestBody WmsStorage wmsStorage) {
        Long userId = getLoginAuthDto().getUserId();
        wmsStorage.setUpdateId(userId);
        wmsStorage.setUpdateTime(new Date());
        wmsStorageService.update(wmsStorage);
        return WrapMapper.ok();
    }
    @PostMapping("/notinstorage")
    @ApiOperation(httpMethod = "POST", value = "查看未入库商品")
    public Wrapper<PageInfo<List<WmsStorageProdDto>>>  getNotInStorage(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                             @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                                       @ApiParam(name = "wmsStorageProdDto", value = "传入分类Id") @RequestBody(required = false) WmsStorageProdDto wmsStorageProdDto){

        PageHelper.startPage(page, size);
        List<WmsStorageProdDto> storageSpec = wmsStorageService.getStorageSpec(wmsStorageProdDto);
        PageInfo pageInfo = new PageInfo(storageSpec);
        return  WrapMapper.ok(pageInfo);
    }

   //查看库存，保质期预警
    @GetMapping("/search")
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<WmsStorage>> searchStorage(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                       @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "20") Integer size,
                                                       @ApiParam(name = "warehouseId", value = "仓库id", defaultValue = "0") @RequestParam(required = false) Long warehouseId,
                                                       @ApiParam(name = "specId", value = "产品Id", defaultValue = "0") @RequestParam(required = false) Long specId,
                                                       @ApiParam(name = "specName", value = "产品名称", defaultValue = "0") @RequestParam(required = false) String specName,
                                                       @ApiParam(name = "categoryId", value = "商品分类id", defaultValue = "0") @RequestParam(required = false) Long categoryId,
                                                       @ApiParam(name = "isEnable", value = "商品启用状态", defaultValue = "0") @RequestParam(required = false) Byte isEnable,
                                                       @ApiParam(name = "beginNumber", value = "库存数量", defaultValue = "0") @RequestParam(required = false) Integer beginNumber,
                                                       @ApiParam(name = "endNumber", value = "库存数量", defaultValue = "0") @RequestParam(required = false) Integer endNumber,
                                                       @ApiParam(name = "qualityAlert", value = "保质期预警参数", defaultValue = "0") @RequestParam(required = false) Integer qualityAlert,
                                                       @ApiParam(name = "stockinAlert", value = "入库天数预警", defaultValue = "0") @RequestParam(required = false) Integer stockinAlert,
                                                       @ApiParam(name = "wareCategory", value = "仓库类别", defaultValue = "0") @RequestParam(required = false) Byte wareCategory
    ) {

        WmsStoragePicDto wmsStoragePicDto = new WmsStoragePicDto();
        if (warehouseId!=null){
            wmsStoragePicDto.setWarehouseId(warehouseId);
        }


        //大于等于
        if (beginNumber != null && !beginNumber.equals("")) {
            wmsStoragePicDto.setBeginNumber(beginNumber);
        }
        //小于等于
        if (endNumber != null && !endNumber.equals("")) {
            wmsStoragePicDto.setEndNumber(endNumber);
        }
        if (specId != null && !specId.equals("")) {
            wmsStoragePicDto.setSpecId(specId);
        }
        if (specName != null && !specName.equals("")) {
            wmsStoragePicDto.setSpecName(specName);
        }
        if (categoryId != null && !categoryId.equals("")) {
            wmsStoragePicDto.setCategoryId(categoryId);
        }
        if (isEnable != null && !isEnable.equals("")) {
            wmsStoragePicDto.setIsEnable(isEnable);
        }
        if (qualityAlert != null && !qualityAlert.equals("") && !qualityAlert.equals(0)) {
            wmsStoragePicDto.setQualityAlert(qualityAlert);
        }

        if (stockinAlert != null && !stockinAlert.equals("") && !stockinAlert.equals(0)) {
            wmsStoragePicDto.setStockinAlert(stockinAlert);
        }
        PageHelper.startPage(page, size);
        List<WmsStorage> wmsStorageList = wmsStorageService.getWmsStorageByCondition(wmsStoragePicDto);
        System.out.println(wmsStorageList);
        if (wmsStorageList == null || wmsStorageList.size() == 0) {
            return WrapMapper.ok();
        }

        List<Long> specIds = wmsStorageList.stream().map(WmsStorage::getSpecId).collect(Collectors.toList());

        List<ProdSpecInfoDto> specInfoDtosBySpecIds = pmcFeignApi.getSpecInfoBySpecIds(specIds);

        if (specInfoDtosBySpecIds != null && specInfoDtosBySpecIds.size() > 0) {
            for (WmsStorage wmsStorage : wmsStorageList){
                for(ProdSpecInfoDto specInfo : specInfoDtosBySpecIds){
                    if (wmsStorage.getSpecId().equals(specInfo.getSpecId())){

                        wmsStorage.setCategoryName(specInfo.getCategoryName());
                        wmsStorage.setProductName(specInfo.getProductName());
                        //wmsStorage.setQuality(specInfo.getQuality());
                        wmsStorage.setSmallPicUri(specInfo.getPicture());
                        wmsStorage.setQuantityDays(DateUtil.getCurrentDays(wmsStorage.getMfgDate()));//出厂天数
                        wmsStorage.setWarehouseDays(DateUtil.getCurrentDays(wmsStorage.getStockinDate()));//入库天数

                    }

                }
            }
        }

        PageInfo pageInfo = new PageInfo(wmsStorageList);
        return WrapMapper.ok(pageInfo);
    }

    private List<Long> getWarehouseIdList(Byte wareCategory){
        if(getGroupInfoDto().getGroupId()!=null ){
            WmsWarehouse wmsWarehouse = new  WmsWarehouse();
            // 登录人为3级部门
            if (getGroupInfoDto().getLevel().toString().equals("3")){
                wmsWarehouse.setGroupId(getGroupInfoDto().getParentId());
            }else{
                wmsWarehouse.setGroupId(getGroupInfoDto().getGroupId());
            }

            if (wareCategory != null && wareCategory > 0){
                wmsWarehouse.setWareCategory(wareCategory);
            }

            List<WmsWarehouseDto>  wmsWarehouseList = wmsWarehouseService.getWarehouseInfoList(wmsWarehouse);

            if (wmsWarehouseList != null && wmsWarehouseList.size()>0){
                List<Long> warehouseIdList = wmsWarehouseList.stream().map(WmsWarehouseDto::getWarehouseId).collect(Collectors.toList());
               return warehouseIdList;
            }
        }
        return null;
    }

    //查看库存，保质期预警
    @PostMapping("/searchMore")
    @ApiOperation(httpMethod = "POST", value = "分页获取所有记录")
    public Wrapper<PageInfo<WmsStorage>> searchMore(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                       @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "20") Integer size,
                                                       @ApiParam(name = "WmsStoragePicDto", value = "dto", defaultValue = "0") @RequestBody(required = false)  WmsStoragePicDto wmsStoragePicDto
    ) {

        if (wmsStoragePicDto.getParentId()!=null&&!wmsStoragePicDto.getParentId().equals((byte)0)){
            ProdCategoryDto prodCategoryDto = new ProdCategoryDto();
            prodCategoryDto.setParentId(wmsStoragePicDto.getParentId());
            prodCategoryDto.setCategoryId(wmsStoragePicDto.getParentId());
            List<Long> list = pmcFeignApi.findLeaf(prodCategoryDto);
            wmsStoragePicDto.setCategoryIdList(list);
            wmsStoragePicDto.setCategoryId(null);
        }

        PageHelper.startPage(page, size);
        List<WmsStorage> wmsStorageList = wmsStorageService.getWmsStorageByCondition(wmsStoragePicDto);
        System.out.println(wmsStorageList);
        if (wmsStorageList == null || wmsStorageList.size() == 0) {
            return WrapMapper.ok();
        }

        List<Long> specIds = wmsStorageList.stream().map(WmsStorage::getSpecId).collect(Collectors.toList());

        //最近一次进货价
        List<WmsStockInDto> purchasePriceList = wmsStockInService.findDescPurchasePrice(specIds);

        List<ProdSpecInfoDto> specInfoDtosBySpecIds = pmcFeignApi.getSpecInfoBySpecIds(specIds);

        if (specInfoDtosBySpecIds != null && specInfoDtosBySpecIds.size() > 0) {
            for (WmsStorage wmsStorage : wmsStorageList){
                for(ProdSpecInfoDto specInfo : specInfoDtosBySpecIds){
                    if (wmsStorage.getSpecId().equals(specInfo.getSpecId())){

                        wmsStorage.setCategoryName(specInfo.getCategoryName());
                        wmsStorage.setProductName(specInfo.getProductName());
                        //wmsStorage.setQuality(specInfo.getQuality());
                        wmsStorage.setSmallPicUri(specInfo.getPicture());
                        wmsStorage.setQuantityDays(DateUtil.getCurrentDays(wmsStorage.getMfgDate()));//出厂天数
                        wmsStorage.setWarehouseDays(DateUtil.getCurrentDays(wmsStorage.getStockinDate()));//入库天数

                    }

                }

                if (purchasePriceList!=null&&purchasePriceList.size()>0){
                    for (WmsStockInDto wmsStockInDto : purchasePriceList) {
                        wmsStorage.setSupplyPrice(wmsStockInDto.getSupplyPrice());
                    }
                }
            }
        }

        PageInfo pageInfo = new PageInfo(wmsStorageList);
        return WrapMapper.ok(pageInfo);
    }

    //添加报溢商品入库
    /*
    只传入warehouseId 和 productId和specId
     */
    @PostMapping("/addOverFlowStorage")
    @LogAnnotation
    @ApiOperation(httpMethod = "POST", value = "根据productId入库")
    public Wrapper addOverFlowStorage(@RequestBody ProdStorageDto prodStorageDto){
        if (prodStorageDto != null){
            if (prodStorageDto.getProductId() == null ){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,"传入参数错误，productId 为 null");
            }
            if (prodStorageDto.getWarehouseId() == null ){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,"传入参数错误，warehouseId 为 null");
            }
            //获得当前产品的所有规格spec的信息
            List<ProdSpecInfoExDto> prodSpecInfoExDtoList = pmcFeignApi.getSpecIds(prodStorageDto);

            //获得当前产品在库存中的信息
            WmsStorage wmsStorage = new WmsStorage();
            wmsStorage.setProductId(prodStorageDto.getProductId());
            wmsStorage.setWarehouseId(prodStorageDto.getWarehouseId());
            List<WmsStorage> wmsStorageList = new ArrayList<>();
            wmsStorageList = wmsStorageService.select(wmsStorage);

            Long userId = getLoginAuthDto().getUserId();

            if (wmsStorageList == null || wmsStorageList.size()<1){
                //库存中没有该产品
                return WrapMapper.wrap(Wrapper.ERROR_CODE,"库存中没有该产品!");
            }else {//有该产品
                for (ProdSpecInfoExDto prodSpecInfoExDto : prodSpecInfoExDtoList) {
                    for (WmsStorage item : wmsStorageList){
                        if (prodSpecInfoExDto.getSpecId().equals(item.getSpecId()) && item.getWarehouseId().equals(prodStorageDto.getWarehouseId())){
                            //当有相同规格specId，即以前入过库，修改信息
                            WmsStorage wss = new WmsStorage();
                            //BeanUtils.copyProperties(item,wss);
                            wss.setStorageId(item.getStorageId());
                            wss.setWarehouseId(item.getWarehouseId());
                            wss.setSpecId(prodSpecInfoExDto.getSpecId());
                            wss.setProductId(prodSpecInfoExDto.getProductId());
                            wss.setCategoryId(prodSpecInfoExDto.getCategoryId());
                            wss.setBrandId(prodSpecInfoExDto.getBrandId());
                            wss.setSpecName(prodSpecInfoExDto.getSpecName());
                            wss.setUpdateId(userId);
                            if (item.getQuality().equals(0) && prodSpecInfoExDto.getQuality() != null && prodSpecInfoExDto.getQuality() != 0){
                                wss.setQuality(prodSpecInfoExDto.getQuality());
                            }
                            wmsStorageService.update(wss);
                            System.out.println("更新："+wss);
                            break;
                        }
                    }
                }

            }
        }

        return WrapMapper.ok();
    }


}
